package cn.achao.csr.chat.core.mq.consumer;

import cn.achao.csr.api.model.dto.PushMessageDTO;
import cn.achao.csr.api.model.enums.WSPushTypeEnum;
import cn.achao.csr.base.constant.RockMqKeyConstants;
import cn.achao.csr.chat.server.ChatServer;
import cn.achao.csr.chat.service.ChatService;
import com.alibaba.fastjson2.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * Description:
 * Author: achao
 */
@Slf4j
@Component
@RequiredArgsConstructor
@RocketMQMessageListener(topic = RockMqKeyConstants.PUSH_TOPIC, consumerGroup = RockMqKeyConstants.PUSH_GROUP, messageModel = MessageModel.BROADCASTING)
public class PushConsumer implements RocketMQListener<PushMessageDTO> {

    private final ChatService chatService;

    @Override
    public void onMessage(PushMessageDTO message) {
        log.info("mq主题csr_websocket_push收到消息：{}", JSON.toJSONString(message));
        WSPushTypeEnum wsPushTypeEnum = WSPushTypeEnum.of(message.getPushType());
        switch (wsPushTypeEnum) {
            case USER:
                message.getUidList().forEach(uid -> {
                    chatService.sendToUid(message.getWsBaseMsg(), uid);
                });
                break;
            case ALL:
                chatService.sendToAllOnline(message.getWsBaseMsg(), null);
                break;
        }
    }
}
